// pages/infoDeskPulish/infoDeskPulish.js
var app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    httpApiImg: app.globalData.httpApiImg,
    formData:{
      publishUserId: '',
      picture:'',
      content:''
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      ['formData.publishUserId']:JSON.parse(wx.getStorageSync('userInfo')).id
    })
  },
  publish:function(e){
    var _this = this;
    wx.request({
      url: app.globalData.httpApiCar + 'tGyInformationDesk/save', 
      data: this.data.formData,
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success: function (res) {
        if (res.data.success) {
          wx.navigateBack()
        }
        else {
          wx.showToast({
            title: res.data.error,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  changeTextarea:function(e){
    this.setData({
      'formData.content':e.detail.value
    })
  },
  uploadImg:function(e) {//头像点击事件
    var that = this;
    wx.chooseImage({
      count: 1, // 默认9
      success: function (res) {
        that.upload(res.tempFilePaths);
      }
    })
  },
  /*上传图片*/
  upload: function (path) {
    var that = this;
    wx.showToast({
      icon: "loading",
      title: "正在上传"
    }),
      wx.uploadFile({
      url: app.globalData.httpApiCar + 'uploadfile/upload1',
        filePath: path[0],
        name: 'file',
        header: { "Content-Type": "multipart/form-data" },
        formData: {},
        success: function (res) {
          if (res.statusCode != 200) {
            wx.showModal({
              title: '提示',
              content: '上传失败',
              showCancel: false
            })
            return;
          }
          var url = JSON.parse(res.data).data;
          that.setData({
            'formData.picture': url[0]
          })
        },
        fail: function (e) {
          wx.showModal({
            title: '提示',
            content: '上传失败',
            showCancel: false
          })
        },
        complete: function () {
          wx.hideToast();  //隐藏Toast
        }
      })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})